<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Columns extends a_base {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_class');
    }

    public function index() {
        $orderkey_list = array('classid', 'classname', 'configid', 'classpropertyid', 'childnum', 'listid', 'adminid', 'addtime', 'isclose');
        $orderkey = $this->input->get('orderkey');
        if (!in_array($orderkey, $orderkey_list)) {
            $orderkey = 'classid';
        }
        $ascdesc = $this->input->get('ascdesc');
        if ($ascdesc != 'asc') {
            $ascdesc = 'desc';
        }
        $parentid = $this->input->get('parentid');
        $parentid = intval($parentid);
        $where = array('parentid' => $parentid);

        $classname = $this->input->get('classname');
        if (isset($_GET['classname']) && !empty($classname)) {
            $where['classname'] = $classname;
        }

        $isclose = $this->input->get('isclose');
        if (isset($_GET['isclose']) && $isclose != -1) {
            $where['isclose'] = $isclose;
        }
        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $cnt = $this->m_class->get_count($where);
        $page_size = 20;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);

        $t_data = array(
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'order' => $orderkey . ' ' . $ascdesc,
        );

        $data['list'] = $this->m_class->get_class_list($t_data);

        $this->load->model('m_class_property');
        $data['class_property'] = $this->m_class_property->get_all(array('column' => 'classpropertyid,propertyname'));

        $this->load->model('m_config');
        $data['config'] = $this->m_config->get_all(array('column' => 'configid,languagever'));

        $this->load->model('m_admin');
        $data['admin'] = $this->m_admin->get_all(array('column' => 'adminname,adminid'));

        $where['orderkey'] = $orderkey;
        $where['ascdesc'] = $ascdesc;
        $data['where'] = $where;
        $data['node_list'] = $this->m_class->get_node($parentid);
        $this->load->view('columns/index', $data);
    }

    function pinyin() {
        $cn = $this->input->get('cn');
        $this->load->library('pinyin');
        $pinyin = $this->pinyin->getAllPY($cn);
        echo json_encode(array('pinyin' => $pinyin));
    }

    function move() {
        $classid = $this->input->get('classid');

        $list = explode('.', $classid);
        $classlist = $this->m_class->get_class_in_id($list);

        if (empty($classlist)) {
            // 提示
            $vt_data = array(
                'js_fun' => 'tips',
                'param' => array('请选择移动栏目', 'index'),
            );
            $this->load->view('tips', $vt_data);
            return;
        }

        if (isset($_POST['btnSave'])) {
            $parentid = intval($this->input->post('parentid'));
            foreach ($classlist as $k => $v) {
                $this->_move_class($v, $parentid);
            }
            redirect('columns/index');
        }
        $cl_data = array(
            'column' => 'classid,classname,parentid',
        );
        $tree_classlist = $this->m_class->get_class_list($cl_data);

        $this->load->library('tree');
        $this->tree->setTree($tree_classlist, 'classid', 'parentid', 'classname');
        $classid_list = array();
        foreach ($classlist as $k => $v) {
            $classid_list[] = $v['classid'];
        }
        $tree = $this->tree->getOptions(0, 0, $classid_list, '|——');
        $v_data['tree'] = $tree;
        $v_data['classlist'] = $classlist;
        $this->load->view('columns/move', $v_data);
    }

    private function _move_class($class, $parentid) {
        //print_r($class);echo $parentid;
        if ($parentid == $class['parentid']) {
            return false;
        }
        $c_data = array('column' => 'classid,classname,parentid,childnum');

        if ($parentid != 0) {
            $c_data['classid'] = $parentid;
            $parent_class = $this->m_class->get_class($c_data);
            if (empty($parent_class)) {
                return false;
            }
        }

        //开始节点移动
        if ($parentid != 0 && $class['parentid'] == 0) {
            // 父节点 移动到 跟节点
            $num = $parent_class['childnum'] + 1;
            $data = array('classid' => $parent_class['classid'], 'childnum' => $num);
            $this->m_class->update($data);
        } else if ($parentid != 0 && $class['parentid'] != 0) {
            // 跟节点 移动到 跟节点
            $c_data['classid'] = $class['parentid'];
            $old_parent_class = $this->m_class->get_class($c_data);
            $num = $old_parent_class['childnum'] - 1 >= 0 ? ($old_parent_class['childnum'] - 1) : 0;
            $data = array('classid' => $old_parent_class['classid'], 'childnum' => $num);
            $this->m_class->update($data);

            $num = $parent_class['childnum'] + 1;
            $data = array('classid' => $parent_class['classid'], 'childnum' => $num);
            $this->m_class->update($data);
        } else if ($parentid == 0 && $class['parentid'] != 0) {
            // 跟节点 移动到 父节点
            $c_data['classid'] = $class['parentid'];
            $old_parent_class = $this->m_class->get_class($c_data);
            $num = $old_parent_class['childnum'] - 1 >= 0 ? ($old_parent_class['childnum'] - 1) : 0;
            $data = array('classid' => $old_parent_class['classid'], 'childnum' => $num);
            $this->m_class->update($data);
        }
        //移动分类
        $cu_data = array('parentid' => $parentid, 'classid' => $class['classid']);
        $this->m_class->update($cu_data);
        return true;
    }

    public function add() {
        $classconfig = array(
            'pagesize' => 12,
            'topnum' => 0,
            'isshowsub' => 0,
            'isonlyrecommend' => 0,
            'orderfield' => '',
            'orderkey' => '',
            'titlenum' => 0,
            'descnum' => 0,
            'datalink' => 'article-details-',
            'styleclass' => '',
        );
        $defalut = array(
            'configid' => 0,
            'classname' => '',
            'enname' => '',
            'classlinkname' => '',
            'classpropertyid' => 0,
            'classtemplateid' => 0,
            'classpic' => '',
            'linkurl' => '',
            'target' => '_self',
            'isgotofirst' => 0,
            'isshownav' => 0,
            'thumbwidth' => 0,
            'thumbheight' => 0,
            'seotitle' => '',
            'keywords' => '',
            'description' => '',
            'classcontent' => '',
            'classconfig' => $classconfig,
            'parentid' => 0,
            'listid' => 1,
            'isclose' => 0,
        );

        $classid = $this->input->get('classid');
        $parentid = $this->input->get('parentid');
        if ($classid > 0) {
            //修改
            $class = $this->m_class->get_class(array('classid' => $classid));
            if (empty($class)) {
                // 提示
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('栏目不存在', 'index'),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            $class['classconfig'] = (Array) json_decode($class['classconfig']);
            $data['post_url'] = b_url('columns/add?classid=' . $classid);
            $data['class'] = $class;
            $parentid = $class['parentid'];
        } else {
            $parentid = $this->input->get('parentid');
            $parentid = intval($parentid);
            if ($parentid != 0) {
                $parent = $this->m_class->get_class(array('classid' => $parentid));
                if (empty($parent)) {
                    $vt_data = array(
                        'js_fun' => 'tips',
                        'param' => array('父级栏目不存在', 'index'),
                    );
                    $this->load->view('tips', $vt_data);
                    return;
                }
            }
            $data['class'] = $defalut;
            $data['post_url'] = b_url('columns/add?parentid=' . $parentid);
        }
        if (isset($_POST['btnSave'])) {
            $post = $this->input->post();

            foreach ($post as $k => $v) {
                if (isset($defalut[$k])) {
                    $defalut[$k] = $post[$k];
                } elseif (isset($post[$k])) {
                    $classconfig[$k] = $post[$k];
                }
            }
            if ($defalut['thumbwidth'] > 0 && $defalut['thumbheight'] > 0) {
                if (file_exists('.' . $defalut['classpic'])) {
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = '.' . $defalut['classpic'];
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['thumb_marker'] = '.thumb';
                    $config['width'] = $defalut['thumbwidth'];
                    $config['height'] = $defalut['thumbheight'];
                    $this->load->library('image_lib', $config);
                    $this->image_lib->resize();
                    //echo $this->image_lib->display_errors();
                }else{
                    $defalut['classpic']='';
                }
            }
            $defalut['classconfig'] = json_encode($classconfig);

            if ($classid > 0) {
                if (isset($post['isupdatesubclassconfig']) && $post['isupdatesubclassconfig'] == 1) {
                    $p_data = array(
                        'classconfig' => $defalut['classconfig'],
                        'parentid' => $parentid
                    );
                    $this->m_class->update_by_parentid($p_data);
                }
                $defalut['classid'] = $class['classid'];
                $defalut['parentid'] = $parentid;
                $this->m_class->update($defalut);
            } else {
                $defalut['parentid'] = $parentid;
                $defalut['adminid'] = $this->session->userdata['user_id'];
                $defalut['addtime'] = date('Y-m-d H:i:s');
                // unset($result['classpropertyid']);
                $this->m_class->insert($defalut);
            }
            if(!$parentid){
                redirect('columns/index');
            }else{
                redirect('columns/index?parentid='.$parentid);
            }
            exit;
        }

        $data['node_list'] = $this->m_class->get_node($parentid);

        $this->load->model('m_config');
        $data['config'] = $this->m_config->get_all(array('column' => 'configid,languagever'));

        $this->load->model('m_class_property');
        $data['class_property'] = $this->m_class_property->get_all(array('column' => 'classpropertyid,propertyname'));

        $data['class_template'] = array();
        if ($classid > 0) {
            $t_data['where'] = array('classpropertyid' => $class['classpropertyid']);
            $t_data['column'] = 'classtemplateid,templatename';
            $this->load->model('m_class_template');
            $data['class_template'] = $this->m_class_template->get_all($t_data);
        }

        $this->load->view('columns/add', $data);
    }

    function delete() {
        $classid = $this->input->get('classid');
        $list = explode('.', $classid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_class->delete(array('classid' => $v));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $classid) . '栏目删除成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function openclose() {
        $classid = $this->input->get('classid');
        $status = $this->input->get('status');
        $str_status = '关闭';
        if ($status != 1) {
            $status = 0;
            $str_status = '开放';
        }
        $list = explode('.', $classid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_class->update(array('classid' => $v, 'isclose' => $status));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $classid) . '栏目' . $str_status . '成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function get_template() {
        $classpropertyid = $this->input->get('classpropertyid');
        $this->load->model('m_class_template');
        $data['where'] = array('classpropertyid' => $classpropertyid);
        $data['column'] = 'classtemplateid,templatename';
        $list = $this->m_class_template->get_class_template_list($data);
        echo json_encode($list);
    }

}
